import axios from "axios";

// create an axios instance
const service = axios.create({
  baseURL: "/mock",
  timeout: 20000 // request timeout
});

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    return config;
  },
  error => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  response => {
    const result = response.data;
    if (result.code === 2100) {
      return result;
    } else {
      return Promise.reject(new Error("请求失败"));
    }
  },
  error => {
    return Promise.reject(error);
  }
);

export default service;
